import flet as ft
import traceback
import os

from views.common import Page,show_snack_bar
from utils.log import logger
from utils.start import main as start_main
from utils.config import config

BG_IMAGE_PATH = os.path.join(os.path.dirname(__file__), "icon", "bg_image.jpg")

def home_view(page: Page):

    def change_all_button(value: bool = True):
        cnt = 0
        for i in page.views[0].controls[0].controls:
            if isinstance(i, ft.FilledButton):
                if cnt < 1:
                    i.disabled = value
                    cnt += 1
                else:
                    i.disabled = False
        page.update()

    def run(func, *args, **kwargs):
        try:
            change_all_button()
            res = func(*args, **kwargs)
            change_all_button(False)
            return res
        except ValueError as e:
            pass
        except Exception:
            print("E: 运行函数时出现错误")
            traceback.print_exc()
        finally:
            change_all_button(False)


    def start(e):
        config.start_Flag = True
        res = run(start_main)
        show_snack_bar(page, "开始运行", "green")
        if config.enable_log_Flag is True:
            logger.info("开始运行")

    def stop(e):
        config.start_Flag = False
        show_snack_bar(page, "停止运行", "red")
        if config.enable_log_Flag is True:
            logger.info("停止运行")

    def go_settings(e):
        page.go("/settings")
        if config.enable_log_Flag is True:
            logger.info("转到设置页面")

    def go_about(e):
        dlg = ft.AlertDialog(
            title=ft.Text("此程序为开发调试版本，如有不足请见谅。"),
            content=ft.Text(
                "点击除提示框显示以外的地方，关闭提示框"
            ),
        )
        page.overlay.append(dlg)
        dlg.open = True
        page.update()
        if config.enable_log_Flag is True:
            logger.info("转到关于页面")

    # # 背景图片容器
    # background = ft.Container(
    #     content=ft.Image(
    #         src="D:/MyFile/Python_Code/Flet_Test/icon/bg_image.jpg", #BG_IMAGE_PATH,
    #         fit=ft.ImageFit.COVER,
    #         opacity=0.5,  # 调整图片透明度
    #         expand=True,
    #         repeat=ft.ImageRepeat.NO_REPEAT,  # 新增
    #         width=650,
    #         height=650,
    #     ),
    #     expand=True,
    #     margin=-10,  # 负边距消除白边
    #     alignment=ft.alignment.center
    # )

    # # 主内容列
    # content_column = ft.Column(
    #     [
    #         ft.Text(
    #             value="Home",
    #             size=50,
    #             color=page.theme.color_scheme.surface,
    #         ),
    #         ft.ElevatedButton(
    #             content=ft.Row(
    #                 [
    #                     ft.Icon("login"),
    #                     ft.Text("运行", weight=ft.FontWeight.W_600),
    #                 ],
    #                 alignment="spaceAround",
    #             ),
    #             on_click=start,
    #             width=120,
    #         ),
    #         ft.ElevatedButton(
    #             content=ft.Row(
    #                 [
    #                     ft.Icon("stop"),
    #                     ft.Text("停止", weight=ft.FontWeight.W_600),
    #                 ],
    #                 alignment="spaceAround",
    #             ),
    #             on_click=stop,
    #             width=120,
    #         ),
    #         ft.ElevatedButton(
    #             content=ft.Row(
    #                 [
    #                     ft.Icon("settings"),
    #                     ft.Text("设置", weight=ft.FontWeight.W_600),
    #                 ],
    #                 alignment="spaceAround",
    #             ),
    #             on_click=go_settings,
    #             width=120,
    #         ),
    #         ft.ElevatedButton(
    #             content=ft.Row(
    #                 [
    #                     ft.Icon("info"),
    #                     ft.Text("关于", weight=ft.FontWeight.W_600),
    #                 ],
    #                 alignment="spaceAround",
    #             ),
    #             on_click=go_about,
    #             width=120,
    #         ),
    #     ],
    #     spacing=20,
    #     alignment="center",
    #     horizontal_alignment="center",
    # )

    # # 使用 Stack 分层布局
    # page.views.append(
    #     ft.View(
    #         "/",
    #         controls=[
    #             ft.Stack(
    #                 [
    #                     background,
    #                     ft.Container(
    #                         content=content_column,
    #                         expand=True,
    #                         padding=20,
    #                         alignment=ft.alignment.center
    #                     )
    #                 ],
    #                 expand=True
    #             )
    #         ],
    #         vertical_alignment="center",
    #         horizontal_alignment="center",
    #     )
    # )


    page.views.append(
        ft.View(
            "/",
            controls=[
                ft.Stack(
                    [
                        ft.Container(
                            content=ft.Image(
                                src=os.path.join(os.getcwd()+"/icon/","bg_image.jpg"), #BG_IMAGE_PATH,
                                fit=ft.ImageFit.COVER,
                                opacity=0.7,  # 调整图片透明度
                                expand=True,
                                repeat=ft.ImageRepeat.NO_REPEAT,  # 新增
                                width=650,
                                height=650,
                            ),
                            expand=True,
                            margin=-10,  # 负边距消除白边
                            alignment=ft.alignment.center
                        ),
                        ft.Container(
                            content=ft.Column(
                                [
                                    ft.Text(
                                        value="Home",
                                        size=50,
                                        color=page.theme.color_scheme.surface,
                                    ),
                                    ft.ElevatedButton(
                                        content=ft.Row(
                                            [
                                                ft.Icon("login"),
                                                ft.Text("运行",weight=ft.FontWeight.W_600),
                                            ],
                                            alignment="spaceAround",
                                        ),
                                        on_click=start,
                                        width=120,
                                    ),
                                    ft.ElevatedButton(
                                        content=ft.Row(
                                            [
                                                ft.Icon("stop"),
                                                ft.Text("停止",weight=ft.FontWeight.W_600),
                                            ],
                                            alignment="spaceAround",
                                        ),
                                        on_click=stop,
                                        width=120,
                                    ),
                                    ft.ElevatedButton(
                                        content=ft.Row(
                                            [
                                                ft.Icon("settings"),
                                                ft.Text("设置",weight=ft.FontWeight.W_600),
                                            ],
                                            alignment="spaceAround",
                                        ),
                                        on_click=go_settings,
                                        width=120,
                                    ),
                                    ft.ElevatedButton(
                                        content=ft.Row(
                                            [
                                                ft.Icon("info"),
                                                ft.Text("关于",weight=ft.FontWeight.W_600),
                                            ],
                                            alignment="spaceAround",
                                        ),
                                        on_click=go_about,
                                        width=120,
                                    ),
                                ],
                                spacing=20,  # 组件间距
                                alignment = "center",
                                horizontal_alignment = "center",
                            ),
                            expand=True,
                            padding=20,
                            alignment=ft.alignment.center
                        ),
                    ],
                    expand=True,
                ),
            ],
            vertical_alignment = "center",
            horizontal_alignment = "center",
            bgcolor=page.theme.color_scheme.background,
        )
    )